package com.shangguigu.hadoopdashujv.pojo;

import org.apache.hadoop.io.*;
import org.apache.hadoop.mapreduce.Reducer;

import java.io.IOException;

public class WordCountReducer extends Reducer<Text, IntWritable, Text, IntWritable> {
    // 重写reduce方法
    @Override
    protected void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException {
        // 初始化sum变量
        int sum = 0;
        // 遍历values中的每个IntWritable
        for (IntWritable value : values) {
            // 将每个IntWritable的值加到sum中
            sum += value.get();
        }
        // 将key和sum写入context中
        context.write(key, new IntWritable(sum));
    }
}
